<?php
include ("config.php");

if ($_POST["save"])
{
    foreach ($_POST["cols"] as $k => $v)
    {
        if ($v["visible"]) $v["visible"] = 1;
        else $v["visible"] = 0;
        
        if (empty($v["pattern"])) $v["pattern"] = "NULL";
        else $v["pattern"] = "'" . trim($v["pattern"]) . "'";
        
        $db->exeQuery("DELETE FROM `dbmister`.`columns`
                       WHERE `database` LIKE '{$GLOBALS["config"]["db"]["database"]}'
                       AND `table` = '{$_POST["table"]}'
                       AND `column` = '{$k}'");
        
        $db->exeQuery("INSERT INTO `dbmister`.`columns`
                       SET `title` = '{$v["title"]}',
                       `note` = '{$v["note"]}',
                       `visible` = '{$v["visible"]}',
                       `pattern` = {$v["pattern"]},
                       `database` = '{$GLOBALS["config"]["db"]["database"]}',
                       `table` = '{$_POST["table"]}',
                       `column` = '{$k}'");
                       
        foreach ($_POST["forkeys"] as $fk => $fv)
        {
            if (empty($fv)) $fv = "NULL";
            else $fv = "'" . trim($fv) . "'";
            $db->exeQuery("UPDATE `dbmister`.`columns`
                           SET forkey_string = {$fv}
                           WHERE `database` LIKE '{$GLOBALS["config"]["db"]["database"]}'
                           AND `table` = '{$_POST["table"]}'
                           AND `column` = '{$fk}'");
        }

    }
    
    $db->exeQuery("DELETE FROM `dbmister`.`tables`
                   WHERE `database` LIKE '{$GLOBALS["config"]["db"]["database"]}'
                   AND `table` = '{$_POST["table"]}'");
    
    $db->exeQuery("INSERT INTO `dbmister`.`tables`
                   SET `title` = '{$_POST["set"]["title"]}',
                   `note` = '{$_POST["set"]["note"]}',
                   `visible` = '{$_POST["set"]["visible"]}',
                   `editable` = '{$_POST["set"]["editable"]}',
                   `type` = '{$_POST["set"]["type"]}',
                   `database` = '{$GLOBALS["config"]["db"]["database"]}',
                   `table` = '{$_POST["table"]}'");
                   
    foreach ($_POST["options"] as $k => $v)
    {
        $db->exeQuery("DELETE FROM `dbmister`.`options`
                       WHERE `database` LIKE '{$GLOBALS["config"]["db"]["database"]}'
                       AND `table` = '{$_POST["table"]}'
                       AND `column` = '{$k}'");
                           
        foreach ($v as $vk => $vv)
        {
            $db->exeQuery("INSERT INTO `dbmister`.`options`
                           SET `title` = '{$vv}',
                           `column` = '{$k}',
                           `option` = '{$vk}',
                           `database` = '{$GLOBALS["config"]["db"]["database"]}',
                           `table` = '{$_POST["table"]}'");
        }
    }
    
    Layout::iframeAlertAndClose("Změny uloženy");
}

Layout::showIframeHeader($db->loadListTables());

Layout::printTableSettings($db->loadDescribe($_GET["table"]), $db->loadTablesSettings());

Layout::showIframeFooter();
?>
